<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <h3>兄弟组件</h3>
        <tom></tom>
        <jerry></jerry>

    </div>
    <script src="../vue.js"></script>
    <script>
        var hub = new Vue();
        Vue.component('tom', {
            data() {
                return {
                    num: 0
                }
            },
            template: `<div>
                            <div>
                                <span>{{num}}</sapn>
                                <button @click="handle">点击增加jerry</button>
                            </div>

                       </div`,
            methods: {
                handle() {
                    hub.$emit("tom", 1)
                }
            },
            mounted() {
                hub.$on("jerry", (val) => {
                    this.num += val
                })
            }
        })
        Vue.component('jerry', {
            data() {
                return {
                    num: 0
                }
            },
            template: `<div>
                            <div>
                                <span>{{num}}</sapn>
                                <button @click="handle">点击增加tom</button>
                            </div>

                       </div`,
            methods: {
                handle() {
                    hub.$emit("jerry", 2)
                }

            },
            mounted() {
                hub.$on("tom", (val) => {
                    this.num += val
                })
            }
        })
        const vm = new Vue({
            el: "#app",
            data: {

            }
        })
    </script>
</body>

</html>